package org.ajax.servlet;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.ajax.entity.Student;

import java.io.IOException;
import java.io.PrintWriter;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/*
 *
 *@author XCH
 *@date2024 - 11 - 20
 *
 */
@WebServlet("/list")
public class ListDateServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");

        List<Student> list = new ArrayList<>();
        Student s1 = new Student("zs", 18, 99.9, new Date());
        Student s2 = new Student("ls", 19, 98.9, new Date());
        Student s3 = new Student("ww", 20, 97.9, new Date());
        list.add(s1);
        list.add(s2);
        list.add(s3);

        Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd").create();
        String json = gson.toJson(list);

        resp.setContentType("application/json; charset=utf-8");

        PrintWriter out = resp.getWriter();
        out.write(json);
        out.flush();
    }
}

